package com.ce.source.analysis.queue;

import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author ：xiaopingzhang8@creditease.cn
 * @Description ：
 * @ClassName ：ConcurrentLinkedQueueAnalyse
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2017/11/29 11:20
 */

/**
 * <h2>ConcurrentLinkedQueue</h2>
 * <ol>
 * <li><span style="color:red ">ConcurrentLinkedQueue</span>是线程安全的，通过cas原子操作保证线程安全，相对于<span style="color:red">LinkedBlockingQueue</span>来说，性能相对要高很多。</li>
 * <li>LinkedBlockingQueue是直接加ReentrantLock(重入锁)来保证线程安全</li>
 * </ol>
 */
public class ConcurrentLinkedQueueAnalyse {
    public static void main(String[] args) {
        analysePut();
    }

    /**
     * <h2>analysePut方法分析</h2>
     * <ol>
     *     <li>
     *       ConcurrentLinkedQueue初始化不能指定容量
     *     </li>
     * </ol>
     */
    public static void analysePut(){
        final ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<Object>();
        queue.offer(1);
        queue.offer(2);
        traverse(queue);
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("poll->"+queue.poll());
            }
        },"num-1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("poll->"+queue.poll());
            }
        },"num-2").start();


    }

    public static void traverse(Queue<Object> queue){
        for (Object aQueue : queue) {
            System.out.print(aQueue + "\t");
        }
    }
}
